package com.ojoin.trade.common_core.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.IExceptionCode;
import com.ojoin.trade.common.utils.UUIDUtils;
import com.ojoin.trade.common.utils.paynow.HttpPost;
import com.ojoin.trade.common.utils.paynow.IAccountConst;
import com.ojoin.trade.common.utils.paynow.Packages;
import com.ojoin.trade.common.utils.paynow.Unpack;
import com.ojoin.trade.common_core.web.service.CardBaseService;

@Service
public class CardBaseServiceImpl implements CardBaseService {

	private Logger log = LoggerFactory
			.getLogger(CardBaseServiceImpl.class);
	
	@Override
	public List bindCard(String mhtNo, String mobile, String cardNo,
			String cardProvince, String cardCity, String branchNo)
			throws BusinessException {
		Packages packages = new Packages();
      Unpack unpack = new Unpack();
      Map<String,String> map = new HashMap<String, String>();
      List list=null;
      try {
      	String requestReport = "";
          String funcode=IAccountConst.TIECARD_FUC;
          map.put("funcode", funcode);
          map.put("mhtNo", mhtNo);
          map.put("mobile",mobile);
          map.put("cardNo", cardNo);//银行卡号
          map.put("cardProvince", cardProvince);//开户行省
          map.put("cardCity", cardCity);//开户行市
          map.put("branchNo", branchNo);//开户行分支号
          String orderId=UUIDUtils.create();
          map.put("orderId",orderId);
          requestReport = packages.returnMess(funcode,map);
          requestReport = "requestReport="+requestReport;
          String xml = HttpPost.http(IAccountConst.IP,requestReport) ;
          list = unpack.unpack(xml, Constants.ENCODING);
          
	}catch(Exception e){
		String errcode = IExceptionCode.CONNECT_CODE;
		String errmsg = "与服务器连接异常";
		log.error(errmsg,e);
		BusinessException be = new BusinessException(errmsg, errcode);
		throw be;
	}
		return list;
	}

	@Override
	public List unBindCard(String mhtNo, String cardId)
			throws BusinessException {
		Packages packages = new Packages();
      Unpack unpack = new Unpack();
      Map<String,String> map = new HashMap<String, String>();
      List list=null;
      try {
      	String requestReport = "";
          String funcode=IAccountConst.RELEASE_FUC;
          map.put("funcode", funcode);
          map.put("mhtNo", mhtNo);
          map.put("cardId", cardId);
          map.put("orderId", new Long(System.currentTimeMillis()).toString());
          requestReport = packages.returnMess(funcode,map);
          requestReport = "requestReport="+requestReport;
          String xml = HttpPost.http(IAccountConst.IP,requestReport) ;
          list = unpack.unpack(xml, Constants.ENCODING);
      }catch(Exception e){
    	String errcode = IExceptionCode.CONNECT_CODE;
  		String errmsg = "与服务器连接异常";
  		log.error(errmsg,e);
  		BusinessException be = new BusinessException(errmsg, errcode);
  		throw be;
      }
      return list;
	}
    //TODO
	@Override
	public List queryBindCardList(String mhtNo)  {
	  Packages packages = new Packages();
      Unpack unpack = new Unpack();
      Map<String,String> map = new HashMap<String, String>();
      List list=null;
      try {
      	String requestReport = "";
          String funcode=IAccountConst.QUERYCARD_FUC;
          map.put("funcode", funcode);
          map.put("mhtNo", mhtNo);
          requestReport = packages.returnMess(funcode,map);
          requestReport = "requestReport="+requestReport;
         // String xml = HttpPost.http("Http://posp.ipaynow.cn:19898",requestReport) ;
          String xml=HttpPost.http(IAccountConst.MHT_IP,requestReport);
          list = unpack.unpack(xml, Constants.ENCODING);
      }catch(Exception e){
    	  String errcode = IExceptionCode.CONNECT_CODE;
    		String errmsg = "与服务器连接异常";
    		log.error(errmsg,e);
    		BusinessException be = new BusinessException(errmsg, errcode);
    		throw be;
      }
      return list;
	}

}
